import {Directive, Input, Output, EventEmitter, HostListener} from "@angular/core";
import {ResponseMsgModel} from "../shared/model/response-msg-model";
import {VerifyCommon} from "./verify-common";
import {CommonHelper} from "../../shared/helper/common-helper";
@Directive({
    selector: "[phoneValidator]"
})
export class VerifyPhoneDirective extends VerifyCommon {
    private static readonly phoneRegex = /^1\d{10}$/;
    @Input() phoneValidator: string;
    @Output() verifyResponse: EventEmitter<ResponseMsgModel> = new EventEmitter<ResponseMsgModel>();

    @HostListener("blur") onBlur() {
        let phone: string = CommonHelper.trim(this.phoneValidator);
        if (!phone) {
            this.responseComponent(false, "电话号码不能为空");
        } else {
            let testResult = VerifyPhoneDirective.phoneRegex.test(phone);
            this.responseComponent(testResult, testResult ? "" : "手机号码格式不正确");
        }
    }

    getResponseEmitter(): EventEmitter<ResponseMsgModel> {
        return this.verifyResponse;
    }
}